<?php
use Entities\Locacao;

include_once '../templates/topo.php';

$id= '';

if(isset($_REQUEST['id']))
	$id = $_REQUEST['id'];
	
$locacao = $em->find("Entities\Locacao", $id);
if(empty($locacao))
	$locacao = new Locacao();
	
?>

	<div class="tabs center">
			<ul>
				<li><a href="#tabs-1">Locacao</a></li>
			</ul>
			<div id="tabs-1">
				<div cols='4'>
					<div class='field' >
						<label>Equipamento</label>
						<input type="hidden" value="<?php echo $id;?>" id='hidId' onkeypress="mascara(this,maiusculo)" />
						<input type='text' id='txtEquipamento' value='<?php echo $locacao->getEquipamento();?>' size='30' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field' >
						<label>Data locacao</label>
						<input type='text' id='txtDataLocacao' value='
							<?php 
								$dataLocacao = $locacao->getDataLocacao();
								if(isset($dataLocacao) && !empty($dataLocacao)){
										echo date('d/m/Y', $dataLocacao->getTimestamp());
									}?>' size='30' onkeypress="mascara(this,maiusculo)" />
						<script type="text/javascript">
							$("#txtDataLocacao").setMask('date');
							$("#txtDataLocacao").datepicker();
						</script>
					</div>
					<div class='field' >
						<label>Fornecedor</label>
						<input type='text' id='txtFornecedor' value='<?php echo $locacao->getFornecedor();?>' size='30' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Obras</label>
						<select id='selObras' >
							<?php 
								$queryObras = $em->createQuery("select c from Entities\Obras c ");
								$obras = $queryObras->getResult();
							
									echo "<option value='0'>Selecione</option>";
									$obraLocacao = $locacao->getObra();
									
									
								foreach ($obras as $obra) {
									if(!empty($obraLocacao)){
										if($locacao->getObra()->getId() == $obra->getId()){
											$selected = "selected='selected'";
										}
									}
									echo "<option value='".$obra->getId()."'".$selected." >".$obra->getNomeObra()."</option>";
								}
							?>
						</select>
					</div>
					<div class='field' >
						<label>Contrato</label>
						<input type='text' id='txtContrato' value='<?php echo $locacao->getContrato();?>' size='30' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field' >
						<label>Data devolucao</label>
						<input type='text' id='txtDataDevolucao' value='
							<?php 
								$dataDevolucao = $locacao->getDataDevolucao();
								if(isset($dataDevolucao) && !empty($dataDevolucao)){
										echo date('d/m/Y', $dataDevolucao->getTimestamp());
									}?>' size='30' onkeypress="mascara(this,maiusculo)" />
						<script type="text/javascript">
							$("#txtDataDevolucao").setMask('date');
							$("#txtDataDevolucao").datepicker();
						</script>
					</div>
					<div class='field' >
						<label>Valor</label>
						<input type='text' id='txtValor' value='<?php echo number_format($locacao->getValor(), 2, ".", "");?>' size='30' onkeypress="mascara(this,tiraVirgula)" />
					</div>
					<div class='field' >
						<label>Status</label>
						<select id="selLocado">
							<option value='0' <?php if($locacao->getLocado() == '0'){echo "selected='selected'";}?> >Locacao</option>
							<option value='1' <?php if($locacao->getLocado() == '1'){echo "selected='selected'";}?> >Devolucao</option>
						</select>
					</div>
				</div>
			</div>
	
			<div class='footer'>
				<button id='btnSalvar'>Salvar</button>
				<script type="text/javascript">
					$("#btnSalvar").click(function(){
						if($('#txtEquipamento').val() == '' || $('#txtDataLocacao').val() == ''|| $('#txtFornecedor').val() == '' || $('#txtContrato').val() == '' ){
							alert('Favor preencher os fornecedor, equipamento, data locacao e contrato', 'Atencao');
						
						}else{
						
							utils.ajax('locacao/salvar', {
								id: $("#hidId").val(),
								equipamento: $('#txtEquipamento').val(),
								dataLocacao: $('#txtDataLocacao').val(),
								fornecedor: $('#txtFornecedor').val(),
								obra: $('#selObras').val(),
								contrato: $('#txtContrato').val(),
								dataDevolucao: $('#txtDataDevolucao').val(),
								valor: $('#txtValor').val(),
								locado: $('#selLocado').val()
								
									
							}, function(xml){
									erro = $(xml).find('erro').text();
									if(parseInt(erro) == 0){
										msg = "Locacao salva com sucesso!";
	
										$("#hidId").val('');
										$('#txtEquipamento').val('');
										$('#txtDataLocacao').val('');
										$('#txtFornecedor').val('');
										$('#txtContrato').val('');
										$('#txtDataDevolucao').val('');
										$('#txtValor').val('');
										$('#selLocado').val('0');
										$('#selLocado').selectmenu();
										$('#selObras').val('0');
										$('#selObras').selectmenu();

										
										
									}
									else{
										msg = "A Locacao NAO foi salva com sucesso!";
									}
									alert(msg, 'Locacao');
							});
						}
					});
				</script>
			</div>
	</div>

<?php 
if(!$popup)
	include_once '../templates/footer.php';
?>